/*
 * MyExperimentSearch.java
 *
 * Created on 08-Jul-2009, 10:19:25
 * Copyright newtonr6
 */
package iffmyexp;

import com.icesoft.faces.async.render.SessionRenderer;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.HtmlGraphicImage;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlPanelGrid;
import com.icesoft.faces.component.ext.UIColumn;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.panellayout.PanelLayout;
import com.icesoft.faces.component.paneltabset.PanelTab;
import com.icesoft.faces.component.paneltabset.PanelTabSet;
import com.sun.rave.faces.data.DefaultSelectItemsArray;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class MyExperimentSearch extends AbstractPageBean {

    static search[] wfSearch;
    static int index;
    static String selectedWorkflow;
    static String desc = "";
    static inputModel[] inputs;
    static inputModel currInput;
    static int count = -1;

    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    private int __placeholder;

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private static HtmlInputText inputText1 = new HtmlInputText();

    public HtmlInputText getInputText1() {
        return inputText1;
    }

    public void setInputText1(HtmlInputText hit) {
        this.inputText1 = hit;
    }
    private HtmlCommandButton button1 = new HtmlCommandButton();

    public HtmlCommandButton getButton1() {
        return button1;
    }

    public void setButton1(HtmlCommandButton hcb) {
        this.button1 = hcb;
    }

    public HtmlGraphicImage thumbnail = new HtmlGraphicImage();

    public HtmlGraphicImage getThumbnail(){
        return thumbnail;
    }

    public void setThumbnail(HtmlGraphicImage hgi){
        this.thumbnail = hgi;
    }

    private String thumb = "/resources/css-images/connect_active.gif";

     public String getThumb() {
        return thumb;
    }

    public void setThumb(String thumb) {
        this.thumb = thumb;
        SessionRenderer.render("wfSession");
    }
    
    private HtmlOutputText groupsOut = new HtmlOutputText();

    public HtmlOutputText getGroupsOut() {
        return groupsOut;
    }

    public void setGroupsOut(HtmlOutputText hot) {
        this.groupsOut = hot;
    }
    private HtmlOutputText workflowsOut = new HtmlOutputText();

    public HtmlOutputText getWorkflowsOut() {
        return workflowsOut;
    }

    public void setWorkflowsOut(HtmlOutputText hot) {
        this.workflowsOut = hot;
    }
    private HtmlOutputText peopleOut = new HtmlOutputText();

    public HtmlOutputText getPeopleOut() {
        return peopleOut;
    }

    public void setPeopleOut(HtmlOutputText hot) {
        this.peopleOut = hot;
    }
    private DefaultSelectedData defaultSelectedData5 = new DefaultSelectedData();

    public DefaultSelectedData getDefaultSelectedData5() {
        return defaultSelectedData5;
    }

    public void setDefaultSelectedData5(DefaultSelectedData dsd) {
        this.defaultSelectedData5 = dsd;
    }
    private DefaultSelectItemsArray selectOneRadio1DefaultItems3 = new DefaultSelectItemsArray();

    public DefaultSelectItemsArray getSelectOneRadio1DefaultItems3() {
        return selectOneRadio1DefaultItems3;
    }

    public void setSelectOneRadio1DefaultItems3(DefaultSelectItemsArray dsia) {
        this.selectOneRadio1DefaultItems3 = dsia;
    }
    private DefaultSelectedData defaultSelectedData6 = new DefaultSelectedData();

    public DefaultSelectedData getDefaultSelectedData6() {
        return defaultSelectedData6;
    }

    public void setDefaultSelectedData6(DefaultSelectedData dsd) {
        this.defaultSelectedData6 = dsd;
    }
    private DefaultSelectItemsArray selectOneMenu1DefaultItems3 = new DefaultSelectItemsArray();

    public DefaultSelectItemsArray getSelectOneMenu1DefaultItems3() {
        return selectOneMenu1DefaultItems3;
    }

    public void setSelectOneMenu1DefaultItems3(DefaultSelectItemsArray dsia) {
        this.selectOneMenu1DefaultItems3 = dsia;
    }
    private HtmlOutputText workflowList = new HtmlOutputText();

    public HtmlOutputText getWorkflowList() {
        return workflowList;
    }

    public void setWorkflowList(HtmlOutputText hot) {
        this.workflowList = hot;
    }
    private HtmlOutputText peopleList = new HtmlOutputText();

    public HtmlOutputText getPeopleList() {
        return peopleList;
    }

    public void setPeopleList(HtmlOutputText hot) {
        this.peopleList = hot;
    }
    private HtmlOutputText groupsList = new HtmlOutputText();

    public HtmlOutputText getGroupsList() {
        return groupsList;
    }

    public void setGroupsList(HtmlOutputText hot) {
        this.groupsList = hot;
    }
    private PanelTab workflowsTab = new PanelTab();

    public PanelTab getWorkflowsTab() {
        return workflowsTab;
    }

    public void setWorkflowsTab(PanelTab pt) {
        this.workflowsTab = pt;
    }
    private PanelTab peopleTab = new PanelTab();

    public PanelTab getPeopleTab() {
        return peopleTab;
    }

    public void setPeopleTab(PanelTab pt) {
        this.peopleTab = pt;
    }
    private PanelTab groupsTab = new PanelTab();

    public PanelTab getGroupsTab() {
        return groupsTab;
    }

    public void setGroupsTab(PanelTab pt) {
        this.groupsTab = pt;
    }
    private HtmlPanelGrid panelGrid1 = new HtmlPanelGrid();

    public HtmlPanelGrid getPanelGrid1() {
        return panelGrid1;
    }

    public void setPanelGrid1(HtmlPanelGrid hpg) {
        this.panelGrid1 = hpg;
    }
    private PanelTabSet resultsTabSet = new PanelTabSet();

    public PanelTabSet getResultsTabSet() {
        return resultsTabSet;
    }

    public void setResultsTabSet(PanelTabSet pts) {
        this.resultsTabSet = pts;
    }
    private PanelLayout panelLayout2 = new PanelLayout();

    public PanelLayout getPanelLayout2() {
        return panelLayout2;
    }

    public void setPanelLayout2(PanelLayout pl) {
        this.panelLayout2 = pl;
    }
    private PanelLayout panelLayout3 = new PanelLayout();

    public PanelLayout getPanelLayout3() {
        return panelLayout3;
    }

    public void setPanelLayout3(PanelLayout pl) {
        this.panelLayout3 = pl;
    }
    private UIColumn col1 = new UIColumn();

    public UIColumn getCol1() {
        return col1;
    }

    public void setCol1(UIColumn uic) {
        this.col1 = uic;
    }
    public static HtmlDataTable workflowsTable = new HtmlDataTable();

    public HtmlDataTable getWorkflowsTable() {
        return workflowsTable;
    }

    public void setWorkflowsTable(HtmlDataTable hdt) {
        this.workflowsTable = hdt;
    }

    public static boolean tableVis = false;

    public boolean gettableVis() {
        return tableVis;
    }

    public void settableVis(boolean hdt) {
        this.tableVis = hdt;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public MyExperimentSearch() {
        SessionRenderer.addCurrentSession("wfSession");

    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     *
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("MyExperimentSearch Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    public void destroy() {
    }

    public String search() {

        tableVis = true;
        String search = (String) inputText1.getText();
        try {
            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = Factory.newDocumentBuilder();
            Document doc = builder.parse("http://www.myexperiment.org/search.xml?query=" + search + "&type=workflow&num=100");
            //creating an XPathFactory:
            XPathFactory factory = XPathFactory.newInstance();
            //using this factory to create an XPath object:
            XPath xpath = factory.newXPath();
            //XPath object created compiles the XPath expression for workflows:
            XPathExpression WFExpr = xpath.compile("//workflow/text()");
            Object WFresult = WFExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList WFnodeList = (NodeList) WFresult;

            XPathExpression WFURIExpr = xpath.compile("//workflow/@uri");
            Object WFresultURI = WFURIExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList WFnodeListURI = (NodeList) WFresultURI;


            wfSearch = new search[WFnodeList.getLength()];
        //perInfo pi = new perInfo(101, "hello", "9891444444", "aaa", 11111);
        //new perInfo(102, "rory", "9911666666", "bbb", 22222),};

            workflowsTab.setLabel("Workflows ("+WFnodeList.getLength()+")");
            for (int i = 0; i < WFnodeList.getLength(); i++) {

                search wf = new search(WFnodeList.item(i).getNodeValue(), WFnodeListURI.item(i).getNodeValue(), "aaa", "fdg");
                wfSearch[i] = wf;
            }

            XPathExpression peopleExpr = xpath.compile("//user/text()");
            Object PeopleResult = peopleExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList PeoplenodeList = (NodeList) PeopleResult;
            //peopleList.setValue(PeoplenodeList.item(0).getNodeValue());

            workflowsTable.setValue(wfSearch);
            /*workflowsOut.setValue("hello");
            workflowsTable.getChildCount();*/

            index = workflowsTable.getRowIndex();


        } catch (Exception e) {
        }

        return "case1";
    }

   /* public void validateEmail(FacesContext context, UIComponent validate, Object value){
        String input = (String)value;

        if(input.indexOf('l')==-1){
            ((UIInput)validate).setValid(false);
            FacesMessage msg = new FacesMessage("'"+input+"'"+" not valid input");
            context.addMessage(validate.getClientId(context), msg);
            tableVis = false;
            workflowsTab.setLabel("Workflows");
        }
    }

    
    

    /*public void createInterface(ActionEvent event) {
        
        InterfaceGenerator.compsVis = true;
        //thumbnail.setValue("");
        thumb = "";
        SessionRenderer.render("wfSession");
        push(event);
    }

     public void push(ActionEvent event) {

        //sort out UI components
        InterfaceGenerator.valuesText.setValue("");
        InterfaceGenerator.outputText3.setValue("");
        //workflowDetails.resetCount();
        InterfaceGenerator.valuesVis = false;
        InterfaceGenerator.textVis = false;
        InterfaceGenerator.listVis = false;

        //put selected workflow's inputs onto InterfaceGenerator
        search[] wfResArray = MyExperimentSearch.wfSearch;
        search wfRes = wfResArray[MyExperimentSearch.workflowsTable.getRowIndex()];
        selectedWorkflow = wfRes.workflow;
        String uri = wfRes.uri;


        try {
            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = Factory.newDocumentBuilder();
            Document doc = builder.parse(uri + "&all_elements=yes");
            //creating an XPathFactory:
            XPathFactory factory = XPathFactory.newInstance();
            //using this factory to create an XPath object:
            XPath xpath = factory.newXPath();
            //XPath object created compiles the XPath expression for workflows:
            XPathExpression descExpr = xpath.compile("//description/text()");
            Object descResult = descExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList descNodeList = (NodeList) descResult;

            XPathExpression inputExpr = xpath.compile("//components/sources/source/name/text()");
            Object inputResult = inputExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList inputNodeList = (NodeList) inputResult;

            XPathExpression thumbExpr = xpath.compile("//thumbnail-big/text()");
            Object thumbResult = thumbExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList thumbNodeList = (NodeList) thumbResult;


            //set workflow variable values from xpath expressions
            desc = descNodeList.item(0).getNodeValue();

            inputs = new inputModel[inputNodeList.getLength()];
            for (int i = 0; i < inputNodeList.getLength(); i++) {
                inputModel inputName = new inputModel(inputNodeList.item(i).getNodeValue(), "a", "b", "c");

                inputs[i] = inputName;
            }

            thumb = thumbNodeList.item(0).getNodeValue();

        } catch (Exception e) {
        }



        InterfaceGenerator.inputsTable.setValue(inputs);
        //InterfaceGenerator.inputsCompleteTable.setValue(workflowDetails.inputs);
        thumb = "";
        SessionRenderer.render("wfSession");
    }






    public void selectOneMenu1_validate(FacesContext context, UIComponent component, Object value) {
    }*/
}